package mylittleinvestigations.CaseInformation;

import java.io.IOException;
import java.util.HashMap;
import mylittleinvestigations.Animation;
import mylittleinvestigations.Area;
import mylittleinvestigations.Conversation;
import mylittleinvestigations.Encounter;
import mylittleinvestigations.Location;

public class Content
{
    private HashMap<String, Area> areaByIdHashMap;
    private HashMap<String, Location> locationByIdHashMap;
    private HashMap<String, Encounter> encounterByIdHashMap;
    private HashMap<String, Conversation> conversationByIdHashMap;
    private HashMap<String, Conversation> conversationSnippetByIdHashMap;
    
    private HashMap<Conversation, Boolean> conversationToOriginalEnabledStateHashMap;

    public Content()
    {
        this.areaByIdHashMap = new HashMap<String, Area>();
        this.locationByIdHashMap = new HashMap<String, Location>();
        this.encounterByIdHashMap = new HashMap<String, Encounter>();
        this.conversationByIdHashMap = new HashMap<String, Conversation>();
        this.conversationSnippetByIdHashMap = new HashMap<String, Conversation>();
    }

    private String initialAreaId;

    public String getInitialAreaId() { return this.initialAreaId; }
    public void setInitialAreaId(String initialAreaId) { this.initialAreaId = initialAreaId; }
    
    private String initialLocationId;

    public String getInitialLocationId() { return this.initialLocationId; }
    public void setInitialLocationId(String initialLocationId) { this.initialLocationId = initialLocationId; }

    public void AddArea(String areaId, Area area)
    {
        this.areaByIdHashMap.put(areaId, area);
    }

    public Area GetAreaFromId(String areaId)
    {
        return this.areaByIdHashMap.get(areaId);
    }

    public void AddLocation(String locationId, Location location)
    {
        this.locationByIdHashMap.put(locationId, location);
    }

    public Location GetLocationFromId(String locationId)
    {
        return this.locationByIdHashMap.get(locationId);
    }

    public void AddEncounter(String encounterId, Encounter encounter)
    {
        this.encounterByIdHashMap.put(encounterId, encounter);
    }

    public Encounter GetEncounterFromId(String encounterId)
    {
        return this.encounterByIdHashMap.get(encounterId);
    }

    public void AddConversation(String conversationId, Conversation conversation)
    {
        this.conversationByIdHashMap.put(conversationId, conversation);
    }

    public Conversation GetConversationFromId(String conversationId)
    {
        return this.conversationByIdHashMap.get(conversationId);
    }
    
    public void AddConversationSnippet(String conversationSnippetId, Conversation conversationSnippet)
    {
        conversationSnippetByIdHashMap.put(conversationSnippetId, conversationSnippet);
    }

    public Conversation GetConversationSnippetFromId(String conversationSnippetId)
    {
        return this.conversationSnippetByIdHashMap.get(conversationSnippetId);
    }
    
    public void Reset()
    {
        for (Conversation conversation : this.conversationByIdHashMap.values())
        {
            conversation.setIsEnabled(this.conversationToOriginalEnabledStateHashMap.get(conversation));
            conversation.getUnlockConditions().clear();
            conversation.getClearedUnlockConditions().clear();
            conversation.setHasBeenCompleted(false);
            conversation.setWrongPartnerUsed(false);
        }
        
        for (Location location : this.locationByIdHashMap.values())
        {
            location.Reset();
        }
        
        for (Area area : this.areaByIdHashMap.values())
        {
            area.Reset();
        }
    }

    void SaveToXml(CaseXmlWriter writer) throws IOException, CaseParseException
    {
        writer.StartElement("Content");
        
        String[] areaIds = this.areaByIdHashMap.keySet().toArray(new String[0]);
        String[] locationIds = this.locationByIdHashMap.keySet().toArray(new String[0]);
        String[] encounterIds = this.encounterByIdHashMap.keySet().toArray(new String[0]);
        String[] conversationIds = this.conversationByIdHashMap.keySet().toArray(new String[0]);
        
        writer.StartElement("AreaByIdHashMap");
        
        for (int i = 0; i < areaIds.length; i++)
        {
            String id = areaIds[i];
            
            writer.StartElement("Entry");
            writer.WriteTextElement("Id", id);
            this.areaByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.StartElement("LocationByIdHashMap");
        
        for (int i = 0; i < locationIds.length; i++)
        {
            String id = locationIds[i];
            
            writer.StartElement("Entry");
            writer.WriteTextElement("Id", id);
            this.locationByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.StartElement("EncounterByIdHashMap");
        
        for (int i = 0; i < encounterIds.length; i++)
        {
            String id = encounterIds[i];
            
            writer.StartElement("Entry");
            writer.WriteTextElement("Id", id);
            this.encounterByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.StartElement("ConversationByIdHashMap");
        
        for (int i = 0; i < conversationIds.length; i++)
        {
            String id = conversationIds[i];
            
            writer.StartElement("Entry");
            writer.WriteTextElement("Id", id);
            this.conversationByIdHashMap.get(id).SaveToXml(writer);
            writer.EndElement();
        }
        
        writer.EndElement();
        
        writer.WriteTextElement("InitialAreaId", this.initialAreaId);
        writer.WriteTextElement("InitialLocationId", this.initialLocationId);
        
        writer.EndElement();
    }

    public Content(CaseXmlReader reader)
    {
        this.areaByIdHashMap = new HashMap<String, Area>();
        this.locationByIdHashMap = new HashMap<String, Location>();
        this.encounterByIdHashMap = new HashMap<String, Encounter>();
        this.conversationByIdHashMap = new HashMap<String, Conversation>();
        this.conversationToOriginalEnabledStateHashMap = new HashMap<Conversation, Boolean>();
    }

    void LoadFromXml(CaseXmlReader reader)
    {
        reader.StartElement("Content");
        
        reader.StartElement("ConversationByIdHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            Conversation conversation = Conversation.LoadFromXml(reader);
            this.conversationByIdHashMap.put(id, conversation);
            this.conversationToOriginalEnabledStateHashMap.put(conversation, conversation.getIsEnabled());
        }
        
        reader.EndElement();
        
        reader.StartElement("EncounterByIdHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            Encounter encounter = new Encounter(reader);
            this.encounterByIdHashMap.put(id, encounter);
        }
        
        reader.EndElement();
        
        reader.StartElement("LocationByIdHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            Location location = new Location(reader);
            this.locationByIdHashMap.put(id, location);
        }
        
        reader.EndElement();
        
        reader.StartElement("AreaByIdHashMap");
        reader.StartList("Entry");
        
        while (reader.MoveToNextListItem())
        {
            String id = reader.ReadTextElement("Id");
            Area area = new Area(reader);
            this.areaByIdHashMap.put(id, area);
        }
        
        reader.EndElement();
        
        this.initialAreaId = reader.ReadTextElement("InitialAreaId");
        this.initialLocationId = reader.ReadTextElement("InitialLocationId");
       
        reader.EndElement();
    }
    
    public void markChildSprites() throws CaseParseException
    {
        for(Location l : locationByIdHashMap.values())
        {
            l.markChildSprites();
        }
    }
}
